package com.firefighting.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 
 * 
 */
public final class JdbcUtils {
	//private static DataSource myDataSource = null;

	private JdbcUtils() {
	}

	/*static {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			Properties prop = new Properties();
			InputStream is = JdbcUtils.class.getClassLoader()
					.getResourceAsStream("dbcpconfig.properties");
			prop.load(is);			
			myDataSource = BasicDataSourceFactory.createDataSource(prop);
		} catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}*/

	/*public static DataSource getDataSource() {
		return myDataSource;
	}*/
//throws SQLException
	
	public static Connection getConnection() {
		// return DriverManager.getConnection(url, user, password);
		//return myDataSource.getConnection();
		String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎
	    String dbURL="jdbc:sqlserver://47.106.213.57:1433;Database=firefighting";//数据源  ！！！！注意若出现加载或者连接数据库失败一般是这里出现问题
	    String Name="sa";
	    String Pwd="ASElab905";
	    Connection conn = null;
	    try {
	    	Class.forName(driverName);
	    	conn=DriverManager.getConnection(dbURL,Name,Pwd);
	    	System.out.println("连接数据库成功");
	    	return conn;
	    }catch(Exception e){
	    	e.printStackTrace();
	    	System.out.println("连接失败");
	    	return null;
	    }

	}

	public static void free(ResultSet rs, Statement st, Connection conn) {
		try {
			if (rs != null)
				rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (st != null)
					st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (conn != null)
					try {
						conn.close();
						// myDataSource.free(conn);
					} catch (Exception e) {
						e.printStackTrace();
					}
			}
		}
	}
}
